#include<iostream>
#include<vector>
#include<queue>
#include<string>

using namespace std;


struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() :val(0), left(nullptr), right(nullptr) {};
    TreeNode(int x) :val(x), left(nullptr), right(nullptr) {};
    TreeNode(int x, TreeNode* left, TreeNode* right) :val(x), left(left), right(right) {};
};



class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        if (!root)return 0;
        queue<pair<TreeNode*, int>>que;
        que.emplace(make_pair(root, 1));
        int cur_ans = 1, max_ans = 1;
        while (!que.empty())
        {
            int n = que.size();
            int start = 1, end = 1;
            for (int i = 0; i < n; i++)
            {
                auto node = que.front();
                que.pop();
                if (node.first->left)
                {
                    que.emplace(make_pair(node.first->left, 2 * node.second));
                    if (i == 0)start = 2 * node.second;
                    if (i == n - 1)end = 2 * node.second;
                }
                if (node.first->right)
                {
                    que.emplace(make_pair(node.first->right, 2 * node.second + 1));
                    if (i == 0)start = 2 * node.second + 1;
                    if (i == n - 1)end = 2 * node.second + 1;
                }
            }
            cur_ans = end - start + 1;
            max_ans = max(cur_ans, max_ans);
        }
        return max_ans;
    }
};

TreeNode* crate_treenode(vector<int*>& num)
{
    if (num.empty() || num[0] == nullptr)return nullptr;
    TreeNode* root = new TreeNode(*num[0]);
    queue<TreeNode*>q;
    q.push(root);
    int i = 1;
    while (!q.empty() && i < num.size())
    {
        TreeNode* node = q.front();
        q.pop();
        if (i < num.size() && num[i] != nullptr)
        {
            node->left = new TreeNode(*num[i]);
            q.push(node->left);
        }
        i++;

        if (i < num.size() && num[i] != nullptr)
        {
            node->right = new TreeNode(*num[i]);
            q.push(node->right);
        }
        i++;
    }
    return root;
}
int main()
{
    vector<string> nums = { "1","3","2" };
    vector<int*>result;
    for (auto num : nums)
    {
        if (num == "null")
        {
            result.emplace_back(nullptr);
        }
        else
        {
            result.emplace_back(new int (stoi(num)));
        }
    }
    TreeNode* root = crate_treenode(result);
    Solution s;
    int ans=s.widthOfBinaryTree(root);
    cout << ans;



    system("pause");
    return 0;
}